package com.ruoyi.web.controller.leaf;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.LeafWorkerId;
import com.ruoyi.system.service.LeafWorkerIdService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @description: TODO
 * @Author: Zhuzhi
 * @Date: 2020/12/13 下午10:10
 */
@Controller
@RequestMapping("/leaf/workerId")
public class LeafWorkerIdController extends BaseController {

    private String prefix = "leaf/workerId";

    @Autowired
    LeafWorkerIdService leafWorkerIdService;


    @RequiresPermissions("leaf:workerId:view")
    @GetMapping()
    public String workerId() {
        return prefix + "/workerId";
    }


    @RequiresPermissions("leaf:workerId:list")
    @PostMapping("/list")
    @ResponseBody
    public List<LeafWorkerId> list(String namespace, String hostIp) {
        List<LeafWorkerId> workerIdList = leafWorkerIdService.selectLeafWorkerIdList(namespace, hostIp);
        return workerIdList;
    }
    /**
     * 新增
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }
    /**
     * 新增
     */
    @RequiresPermissions("leaf:workerId:add")
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated LeafWorkerId leafWorkerId) {

        if (null != leafWorkerIdService.selectLeafWorkerId(leafWorkerId.getNamespace(), leafWorkerId.getHostIp(), leafWorkerId.getWorkerId())) {
            return error("新增序列'" + leafWorkerId.getNamespace() + "'失败，namespace名称已存在");
        }
        return toAjax(leafWorkerIdService.insertLeafWorkerId(leafWorkerId));
    }

    /**
     * 删除
     */
    @RequiresPermissions("leaf:workerId:remove")
    @GetMapping("/remove/{id}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("id") Long id) {
        return toAjax(leafWorkerIdService.deleteLeafWorkerById(id));
    }
}
